package com.hdoit.kylin.web.group.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.baseInfo.service.IAcademyService;
import com.hdoit.kylin.logical.group.model.Group;
import com.hdoit.kylin.logical.group.service.IGroupService;
import com.hdoit.kylin.logical.viewmodel.GroupSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

@Controller
@RequestMapping(value="/admin/group/")
public class GroupController extends BaseController{
	@Override
	public String getRoutePath() {
		return "areas/admin/group/";
	}

	private IGroupService groupService;
	private IAcademyService academyService;

	public IAcademyService getAcademyService() {
		return academyService;
	}
	@Resource
	public void setAcademyService(IAcademyService academyService) {
		this.academyService = academyService;
	}

	public IGroupService getGroupsService() {
		return groupService;
	}

	@Resource
	public void setGroupService(IGroupService groupService) {
		this.groupService = groupService;
	}

	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String groupDetailedViewName="groupDetailed";
	private final String myGroupViewName="myGroup";
	private final String saveViewName="save";
	private final String deleteViewName="delete";
	
	@RequestMapping(value=indexViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String index(Model model,GroupSearch groupSearch,PagerModel pagerModel,boolean ajaxPost){

		pagerModel.setTotalAmount(groupService.queryCount(groupSearch));
		List<Group> groups=groupService.queryList(groupSearch,pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY,pagerModel);
		model.addAttribute("groups",groups);
		model.addAttribute("groupSearch",groupSearch);
		
		
		if(ajaxPost){
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value=groupDetailedViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String groupDetailed(Model model,String key){
		String academyId = null;
		Group group;
		if(StringUtils.isEmpty(key)){
			group=new Group();
		}
		else{
			group=groupService.queryModel(key);
			academyId = group.getAcademyId();
			group.setSchoolId(academyService.queryModel(academyId).getSchoolId());
			group.setAcademyId(academyId);
		}
		
		model.addAttribute("group", group);
		
		return view(groupDetailedViewName);
	}
	
	@RequestMapping(value=saveViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String save(Model model,Group group){
		if(StringUtils.isEmpty(group.getGroupId())){
			group.setGroupId(CommonUtils.getKeyId());
			group.setCreateId(getLoginModel().getLoginId());
			group.setCreateName(getLoginModel().getLoginName());
			group.setCreateDate(DateUtils.getCurrent());
			group.setModelStatus(ModelStatus.add);
			//添加课题组时学院id
			group.setAcademyId("huakeruanyuan");
		}
		else{
			//编辑课题组时学院id
			group.setModelStatus(ModelStatus.update);
		}
		
		ActionResult actionResult;
		if(groupService.applyChange(group)){
			actionResult=new ActionResult();
		}
		else{
			actionResult=new ErrorResult();
		}
		
		model.addAttribute("group", group);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(myGroupViewName);
	}
	
	@RequestMapping(value=deleteViewName,method={RequestMethod.GET,RequestMethod.POST},produces = "text/json;charset=UTF-8")
	public @ResponseBody String delete(String key){
		if(groupService.deleteModel(key)){
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
	
	@RequestMapping(value=myGroupViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String myGroup(Model model){
//		Group myGroup=groupService.queryModelByLoginId(getLoginModel().getLoginId());
//		if(null==myGroup)
//			myGroup=new Group();
//		
//		model.addAttribute("group", myGroup);
//		
//		return view(myGroupViewName);
		return view(outputViewName);
	}
}
